var _n6;
var cc__extends = __extends;
var cc__decorate = __decorate;
var cc__awaiter = __awaiter;
var cc__generator = __generator;
var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $1$EventManager = require("EventManager");
var l = cc__importDefault($1$EventManager);
var $1$yyComponent = require("yyComponent");
var u = cc__importDefault($1$yyComponent);
var $1$userService = require("userService");
var p = cc__importDefault($1$userService);
var $zqcc_OverallSystem = require("zqcc_OverallSystem");
var $GameEventType = require("GameEventType");
var $1$xinshouyindao = require("xinshouyindao");
var f = cc__importDefault($1$xinshouyindao);
var $1$LevelsConfig = require("LevelsConfig");
var y = cc__importDefault($1$LevelsConfig);
var $1$GamePlay = require("GamePlay");
var g = cc__importDefault($1$GamePlay);
var $1$ResourceLoader = require("ResourceLoader");
var v = cc__importDefault($1$ResourceLoader);
var $1$TargetMgr = require("TargetMgr");
var m = cc__importDefault($1$TargetMgr);
var $1$gameConfig1 = require("gameConfig1");
var _ = cc__importDefault($1$gameConfig1);
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var def_Feibiao = function (t) {
  function _ctor() {
    var instantiate = null !== t && t.apply(this, arguments) || this;
    instantiate.targetMgr = null;
    instantiate.xinshouyindaoPre = null;
    instantiate.fuhuoNode = null;
    instantiate.tartget_node = null;
    instantiate.paintNode = null;
    instantiate.levelText = null;
    instantiate.nowGuanqiaNode = null;
    instantiate.donghuaNode = null;
    instantiate.xinshouyindao = null;
    instantiate.level = 1;
    instantiate.lianjitime = 3;
    instantiate.lianjiNumber = 0;
    instantiate.starsNumber = 0;
    return instantiate;
  }
  cc__extends(_ctor, t);
  _ctor.prototype.onLoad = function () {
    this.node.getComponent(cc.Canvas).fitHeight = p.default.IPAD;
    this.node.getComponent(cc.Canvas).fitWidth = !p.default.IPAD;
    cc.game.on("btn_clear_tmp", this.showSaoba, this);
  };
  _ctor.prototype.start = function () {
    const START_GUIDE_TEXT = "新手引导";
    const DEFAULT_DURATION = 300;

    let userData = $zqcc_OverallSystem.sers().userSrv.getFeibiaoData();
    this.level = userData.level;

    let levelText = this.level < 1 ? START_GUIDE_TEXT : `第 ${this.level} 关`;
    this.levelText.string = levelText;

    let levelOrder = y.default.LevelOrder;
    if (window.RemoteLevelConfig && (window.wx || window.tt)) {
      levelOrder = window.RemoteLevelConfig.LevelOrder;
    }

    let adjustedLevel = this.level > _.default.ROPE_MAX_LEVEL
      ? (this.level - 1) % _.default.ROPE_MAX_LEVEL + 1
      : this.level;

    let resourceId = levelOrder[adjustedLevel];
    cc.log("resId", resourceId);
    this.loadLevel(this.level, resourceId);

    $zqcc_OverallSystem.mgrs().soundMgr.playMusic("bgm");

    if (window["GM-mode"]) {
      let buttonNames = ["tiaoguanBtn", "btn_autopaly"];
      buttonNames.forEach(name => {
        this.node.getChildByName(name).active = true;
      });
    }

    $zqcc_OverallSystem.sers().platformSrv.platformApi.recordedGameScreenStart({
      duration: DEFAULT_DURATION,
      isMarkOpen: true
    });

  };
  _ctor.prototype.initxiaoguoNode = function () {
    var _donghua = new cc.Node("donghuaNode");
    _donghua.parent = this.node;
    _donghua.zIndex = 999;
    this.donghuaNode = _donghua;
  };
  _ctor.prototype.loadingLevel = function () {
    const GAME_NAME = "绳子拼图";
    const SCREW_REMOVE_EVENT = "screw_remove";
    const ANIMATION_PREFIX = "animation";
    const INSTRUCTION_STRINGS = [
      "先观察订单颜色",
      "点击对应颜色绳子",
      "匹配不上颜色的绳子会暂时存放在这里",
      "完成拼图则游戏胜利"
    ];

    const t = this;
    const analyticsData = {
      game_name: GAME_NAME,
      level: this.level
    };
    $zqcc_OverallSystem.sers().platformSrv.platformApi.reportAnalytics("game_enter", analyticsData);

    if (this.level === 0) {
      this.xinshouyindao = cc.instantiate(this.xinshouyindaoPre);
      cc.log("引导");
      this.scheduleOnce(() => {
        const lastChild = t.nowGuanqiaNode.children[t.nowGuanqiaNode.childrenCount - 1];
        const targetNode = t.node.getChildByName("tartget_node").getChildByName("target_layout").children[0];
        const tmpFrameNode = t.node.getChildByName("tmpNode").getChildByName("tmp_frame");

        const xinshouyindaoComponent = t.xinshouyindao.getComponent(f.default);
        xinshouyindaoComponent.nodeArr = [targetNode, lastChild, tmpFrameNode, lastChild];

        l.default.on($GameEventType.EventType.DirectorEvent.xinshouyindaoEnd, () => {
          if (t.node && t.xinshouyindao) {
            t.xinshouyindao.parent = null;
          }
        }, t);

        cc.game.on(SCREW_REMOVE_EVENT, t.emitXinshouyindao, t);

        xinshouyindaoComponent.str = INSTRUCTION_STRINGS;
        xinshouyindaoComponent.fx = [2, 2, 2, 2, 2, 2];
        xinshouyindaoComponent.chumo = -999;
        t.xinshouyindao.parent = t.node;
        t.xinshouyindao.zIndex = 13;
        t.xinshouyindao.active = true;
      });
    }

    if (this.level !== 0) {
      this.scheduleOnce(() => {
        t.node.getChildByName("EffectLayer").getChildByName("nandubiaosheng").active = false;
      }, 2.5);
      $zqcc_OverallSystem.mgrs().soundMgr.playSound("ding");

      const nandubiaoshengNode = t.node.getChildByName("EffectLayer").getChildByName("nandubiaosheng");
      nandubiaoshengNode.active = true;

      let animationSuffix = "";
      if (this.level > 1) {
        animationSuffix = (this.level - 1).toString();
      }
      if (this.level > 5) {
        animationSuffix = "4";
      }

      const animationName = `${ANIMATION_PREFIX}${animationSuffix}`;
      nandubiaoshengNode.getComponent(sp.Skeleton).setAnimation(0, animationName, false);
    }

  };
  _ctor.prototype.emitXinshouyindao = function () {
    this.emit($GameEventType.EventType.DirectorEvent.xinshouyindao);
  };
  _ctor.prototype.gameOver = function () {
    const FULL_SLOT_MESSAGE = "槽位已满!";
    const GAME_NAME = "绳子拼图";

    cc.log("over");
    const canUnlock = this.targetMgr.canUnlock();
    cc.find("content/tips1", this.fuhuoNode).active = !canUnlock;
    cc.find("content/tips2", this.fuhuoNode).active = canUnlock;

    this.fuhuoNode.getComponent(cc.Widget).updateAlignment();

    const progressPercentage = Math.floor(100 * this.getProgress()).toFixed(0);
    cc.find("content/textLayout/progressLabel", this.fuhuoNode).getComponent(cc.Label).string = progressPercentage + "%";

    this.fuhuoNode.active = true;
    this.fuhuoNode.scale = 0;

    cc.tween(this.fuhuoNode)
      .call(() => {
        $zqcc_OverallSystem.sers().uiSrv.showMessage(FULL_SLOT_MESSAGE);
      })
      .delay(1.5)
      .to(0.2, { scale: 1 })
      .start();

    const analyticsData = {
      game_name: GAME_NAME,
      level: this.level,
      progress: progressPercentage
    };
    $zqcc_OverallSystem.sers().platformSrv.platformApi.reportAnalytics("game_level_failed", analyticsData);

  };
  _ctor.prototype.getProgress = function () {
    return this.targetMgr.getProgress();
  };
  _ctor.prototype.buxuyao = function () {
    $zqcc_OverallSystem.mgrs().soundMgr.playBtnSound();
    this.fuhuoNode.active = false;
    this.overPage();
  };
  _ctor.prototype.huode = function () {
    const GAME_UI_LAYER_COMPONENT = "GameUILayer";
    const GAME_NAME = "绳子拼图";
    const PROP_NAME = "复活";

    const self = this;
    $zqcc_OverallSystem.mgrs().soundMgr.playBtnSound();
    const canUnlock = this.targetMgr.canUnlock();
    const gameUILayer = this.node.getChildByName("UILayer").getComponent(GAME_UI_LAYER_COMPONENT);

    if (gameUILayer.freeNumber > 0) {
      handleUnlock(canUnlock);
      gameUILayer.freeNumber--;
      $zqcc_OverallSystem.sers().userSrv.setMianfeidaojuData({ cishu: 0 });
    } else {
      $zqcc_OverallSystem.sers().platformSrv.platformApi.loadAndShowVideoAd(
        () => {
          handleUnlock(canUnlock);
          self.fuhuoNode.active = false;
        },
        () => { },
        null,
        {
          game_name: GAME_NAME,
          prop_name: PROP_NAME,
          level: self.level,
          slevel: self.level
        }
      );
    }

    function handleUnlock(isUnlock) {
      // Emit corresponding event based on unlock status
      cc.game.emit(isUnlock ? "btn_clear_tmp" : "unlock_2");
      self.fuhuoNode.active = false;

      // Deactivate all mianfei nodes
      gameUILayer.mianfeiNode.forEach(node => {
        node.active = false;
      });
    }

  };
  _ctor.prototype.xiayiguan = function () {
    $zqcc_OverallSystem.mgrs().soundMgr.playSound("succes");
    var t = {
      level: this.level,
      gameProgress: 1,
      paintNode: this.paintNode
    };
    $zqcc_OverallSystem.sers().uiSrv.showOverPage({
      pass: true,
      data: t
    });
  };
  _ctor.prototype.overPage = function () {
    var t = {
      level: this.level,
      gameProgress: this.getProgress(),
      paintNode: this.paintNode
    };
    $zqcc_OverallSystem.mgrs().soundMgr.playSound("shibai");
    $zqcc_OverallSystem.sers().uiSrv.showOverPage({
      pass: false,
      data: t
    });
  };
  _ctor.prototype.pass = function () {
    const GAME_NAME = "绳子拼图";
    const ANALYTICS_EVENT = "game_level_pass";
    const MAX_ROPE_LEVEL = _.default.ROPE_MAX_LEVEL;

    cc.log("pass");
    $zqcc_OverallSystem.mgrs().soundMgr.playSound("shengli");

    const analyticsData = {
      game_name: GAME_NAME,
      level: this.level
    };
    $zqcc_OverallSystem.sers().platformSrv.platformApi.reportAnalytics(ANALYTICS_EVENT, analyticsData);

    const feibiaoData = $zqcc_OverallSystem.sers().userSrv.getFeibiaoData();
    feibiaoData.selectedPainting = Math.min(feibiaoData.level, MAX_ROPE_LEVEL); // 确保不超过最大等级
    feibiaoData.level++;

    $zqcc_OverallSystem.sers().userSrv.setFeibiaoData(feibiaoData);
    this.xiayiguan();

    seeg.setUserData({
      level: feibiaoData.level
    });

  };
  _ctor.prototype.loadAndShowVideoAd = function (t, e) {
    t.level = this.level;
    $zqcc_OverallSystem.sers().platformSrv.platformApi.loadAndShowVideoAd(function () {
      e && e();
    }, function () { }, null, t);
  };
  _ctor.prototype.setBtn = function () {
    $zqcc_OverallSystem.mgrs().soundMgr.playBtnSound();
    $zqcc_OverallSystem.sers().uiSrv.showSettingPage(1);
  };
  _ctor.prototype.addlianji = function () {
    const LianjiNodeName = "lianjishu";
    const LianjiChildName = "lianji";
    const ProgressBarName = "pb";

    this.lianjiNumber++;

    const lianjiNode = this.node.getChildByName(LianjiNodeName);
    const lianjiLabel = lianjiNode.getChildByName(LianjiChildName);
    lianjiLabel.stopAllActions();
    lianjiLabel.scale = 1;
    lianjiLabel.getComponent(cc.Label).string = "x" + this.lianjiNumber;

    // Tween animation for lianji label
    cc.tween(lianjiLabel)
      .to(0.2, { scale: 1.2 })
      .to(0.1, { scale: 1 })
      .start();

    if (lianjiNode && this.lianjiNumber > 1) {
      lianjiNode.active = true;

      const progressBar = lianjiNode.getChildByName(ProgressBarName).getComponent(cc.ProgressBar);
      if (progressBar) { // 确保 ProgressBar 组件存在
        progressBar.node.stopAllActions();
        progressBar.progress = 1;

        // Tween animation for progress bar
        cc.tween(progressBar)
          .to(this.lianjitime, { progress: 0 })
          .start();
      }
    }

    // 取消并重新调度连击计时器
    this.unschedule(this.congzhilianji);
    this.scheduleOnce(this.congzhilianji, this.lianjitime);

  };
  _ctor.prototype.showSaoba = function () {
    var _saoba = this.tartget_node.getChildByName("saoba");
    _saoba.active = true;
    _saoba.x = 50;
    $zqcc_OverallSystem.mgrs().soundMgr.playSound("saoba");
    cc.tween(_saoba).to(.5, {
      x: 50
    }).call(function () {
      _saoba.active = false;
    }).start();
  };
  _ctor.prototype.congzhilianji = function () {
    this.lianjiNumber = 0;
    this.node.getChildByName("lianjishu").active = false;
  };
  _ctor.prototype.loadLevel = function (t, e) {

    // return cc__awaiter(this, undefined, undefined, function () {
    //   var o;
    //   var n;
    //   var i = this;
    //   return cc__generator(this, function (r) {
    //     switch (r.label) {
    //       case 0:
    //         cc.log("loadLevel", t, e);
    //         r.label = 1;
    //       case 1:
    //         r.trys.push([1, 3,, 4]);
    //         return [4, v.default.getInstance().load("Feibiao", "prefabs/levels/lv_" + e, cc.Prefab)];
    //       case 2:
    //         if (o = r.sent()) {
    //           this.scheduleOnce(function () {
    //             return cc__awaiter(i, undefined, undefined, function () {
    //               var t;
    //               var n;
    //               return cc__generator(this, function () {
    //                 (t = cc.instantiate(o)).position = cc.v3(0, 0, 0);
    //                 (n = this.node.getComponent(g.default)).levelRoot.addChild(t, 3);
    //                 n.boardLayer = t;
    //                 n.level = this.level;
    //                 n.initGame(e, true);
    //                 this.nowGuanqiaNode = t;
    //                 this.node.getChildByName("UILayer").getComponent("GameUILayer").initUI();
    //                 this.node.getChildByName("UILayer").getComponent("GameUILayer").initFeibiao(this);
    //                 this.initxiaoguoNode();
    //                 this.loadingLevel();
    //                 return [2];
    //               });
    //             });
    //           });
    //         } else {
    //           cc.error("关卡加载出错: 未找到预制体");
    //         }
    //         return [3, 4];
    //       case 3:
    //         n = r.sent();
    //         cc.error("关卡加载失败:", n);
    //         return [3, 4];
    //       case 4:
    //         return [2];
    //     }
    //   });
    // });


    return cc__awaiter(this, undefined, undefined, function () {
      let prefabInstance;
      let error;

      const self = this;
      return cc__generator(this, function (r) {
        switch (r.label) {
          case 0:
            cc.log("loadLevel", t, e);
            r.label = 1;
          case 1:
            r.trys.push([1, 3, , 4]);
            return [4, v.default.getInstance().load("Feibiao", "prefabs/levels/lv_" + e, cc.Prefab)];
          case 2:
            prefabInstance = r.sent();
            if (prefabInstance) {
              this.scheduleOnce(function () {
                return cc__awaiter(self, undefined, undefined, function () {

                  var t;
                  var n;
                  return cc__generator(this, function () {
                    (t = cc.instantiate(prefabInstance)).position = cc.v3(0, 0, 0);
                    (n = this.node.getComponent(g.default)).levelRoot.addChild(t, 3);
                    n.boardLayer = t;
                    n.level = this.level;
                    n.initGame(e, true);
                    this.nowGuanqiaNode = t;
                    this.node.getChildByName("UILayer").getComponent("GameUILayer").initUI();
                    this.node.getChildByName("UILayer").getComponent("GameUILayer").initFeibiao(this);
                    this.initxiaoguoNode();
                    this.loadingLevel();
                    return [2];
                  });


                  // const levelNode = cc.instantiate(prefabInstance);
                  // levelNode.position = cc.v3(0, 0, 0);

                  // const gameComponent = this.node.getComponent(g.default);
                  // gameComponent.levelRoot.addChild(levelNode, 3);
                  // gameComponent.boardLayer = levelNode;
                  // gameComponent.level = this.level;
                  // gameComponent.initGame(e, true);
                  // this.nowGuanqiaNode = levelNode;

                  // const uiLayer = this.node.getChildByName("UILayer").getComponent("GameUILayer");
                  // uiLayer.initUI();
                  // uiLayer.initFeibiao(this);

                  // this.initxiaoguoNode();
                  // this.loadingLevel();
                  // return [2];
                });
              });
            } else {
              cc.error("关卡加载出错: 未找到预制体");
            }
            return [3, 4];
          case 3:
            error = r.sent();
            cc.error("关卡加载失败:", error);
            return [3, 4];
          case 4:
            return [2];
        }
      });
    });

  };
  cc__decorate([ccp_property({
    type: m.default,
    tooltip: "目标管理器"
  })], _ctor.prototype, "targetMgr", undefined);
  cc__decorate([ccp_property({
    type: cc.Prefab,
    tooltip: "新手引导预制体"
  })], _ctor.prototype, "xinshouyindaoPre", undefined);
  cc__decorate([ccp_property({
    type: cc.Node,
    tooltip: "复活节点页面"
  })], _ctor.prototype, "fuhuoNode", undefined);
  cc__decorate([ccp_property({
    type: cc.Node,
    tooltip: "目标节点"
  })], _ctor.prototype, "tartget_node", undefined);
  cc__decorate([ccp_property({
    type: cc.Node,
    tooltip: "画板节点"
  })], _ctor.prototype, "paintNode", undefined);
  cc__decorate([ccp_property({
    type: cc.Label,
    tooltip: "等级文本"
  })], _ctor.prototype, "levelText", undefined);
  return cc__decorate([ccp_ccclass], _ctor);
}(u.default);
exports.default = def_Feibiao;